﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Interfaces;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Scanner
{
    /// <summary>
    /// 民德扫码器（支持一维码，二维码，主机模式，等...）
    /// </summary>
    public class MindeoSerialPort : SerialPortBase, IScanner
    {

        public string Name { get; set; }
        public MindeoSerialPort(UseDevice device) : base(device.port, Convert.ToInt32(device.parameter), timeout: 1500)
        {
            Name = device.name;
        }
        /// <summary>
        /// 在主机模式下执行一次
        /// </summary>
        /// <param name="keepTime">保持时长（毫秒）</param>
        /// <returns></returns>
        public Result<string> ReadOne(int timeout)
        {
            lock (this)
            {
                localPort.ReadTimeout = timeout;
                localPort.WriteTimeout = timeout;
                AppendInfo(Name, "扫码");
                var result = WriteThenRead(Encoding.UTF8.GetString(CmdStart));
                AppendResult(Name, result.Value ?? "空");
                return result;
            }
        }

        byte[] CmdStart => new byte[] { 0x16, 0x54, 0x0D };//开始扫描
        byte[] CmdEnd => new byte[] { 0x16, 0x55, 0x0D };//取消扫描
    }
}
